# -*- coding:utf-8 -*-
# @FileName  :t4.py
# @Time      :2021/5/28 16:58
import base64
import json
import hashlib
import requests
from lxml import html

etree = html.etree
kv = {'User-Agent': 'yuanrenxue.project', 'cookie': 'sessionid=3huhl339ssxaj9fv7e6x77cf9p25feh7'}
number_base = {
    '': 0,
    '': 1,
    '': 2,
    '': 3,
    '': 4,
    '': 5,
    '': 6,
    '': 7,
    '': 8,
    '': 9
}


def jie(o):
    c = str(o)
    encode_str = base64.encodebytes(c.encode('utf8'))  # b'hello world!'
    return encode_str.decode()


def mm(l):
    global k1, cojnt
    data = []
    r = requests.get(f'http://match.yuanrenxue.com/api/match/4?page={l}', headers=kv).text
    r1 = json.loads(r)
    input(r1)
    cl = jie(r1['key'] + r1['value']).replace('=', '')  # base64转码
    input(cl)
    cll = hashlib.md5(cl.strip().encode(encoding='UTF-8')).hexdigest()
    input(cll)
    p = r1['info'].split('</td>')
    for k in p[:-1]:
        data.append(k + '</td>')
    n = 1
    for i in data:
        k1 = {}
        soup = etree.HTML(i)
        b = 1
        for j, z, f in zip(soup.xpath('//img/@class'), soup.xpath('//img/@style'), soup.xpath('//img/@src')):
            if j.find(cll) != -1:
                continue
            chu(b, z, f)
            b += 1
        num = ''
        for x in range(len(k1)):
            # print(k1[x + 1])
            num += str(k1[x + 1])
        cojnt += int(num)
        n += 1


def chu(a, c, src):
    '''
    :param a: 图片顺序  自增加一
    :param c: 图片样式  位置
    :param src: 图片映射
    :return: 字典返回排序好的数据
    '''
    global k1
    a = float(a)
    b = float(c.replace('left:', "").replace('px', "")) / 9
    k1[int(a + b)] = number_base[src]


if __name__ == "__main__":
    cojnt = 0
    for i in range(1, 6):
        mm(i)
    print(cojnt)
